package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/11  15:41
 *  @描述       ${TODO}
 */


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.StaffAdministrationBean;
import com.yanq.liancar.bean.StaffMsgBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StaffEditActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_account_type)
    TextView tvAccountType;
    @BindView(R.id.ll_account_type)
    LinearLayout llAccountType;
    @BindView(R.id.et_account_name)
    EditText etAccountName;
    @BindView(R.id.et_phoneNum)
    EditText etPhoneNum;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.tv_Difference)
    TextView tvDifference;
    private int id,roleId;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staffedit);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).init();

        mTvTitle.setText("员工编辑");

        id=getIntent().getIntExtra("id",0);
        //员工编辑
        if (id!=0){
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(R.mipmap.delete);
            getStaffMessages();
        }

        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buyVipDialog();
            }
        });

        SpannableString spannableString = new SpannableString("员工账号和店长账号的区别，点击查看");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.main_color));
        spannableString.setSpan(colorSpan, 13, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan colorSpan3 = new ForegroundColorSpan(getResources().getColor(R.color.background_content));
        spannableString.setSpan(colorSpan3, 0, 13, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvDifference.setText(spannableString);



    }

    @OnClick({R.id.ll_account_type, R.id.tv_save, R.id.tv_Difference})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_account_type:
                myDialog();
                break;
            case R.id.tv_save:
                // 隐藏软键盘
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                if (id!=0){
                    initOkgo();
                }else {
                    initOkgoAdd();
                }
                break;
            case R.id.tv_Difference:
                RxActivityTool.skipActivity(this, StaffHelpActivity.class);
                break;
        }
    }

        private void myDialog(){
            final Dialog bottomDialog = new Dialog(this);
            View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_account_type, null);
            bottomDialog.setContentView(contentView);
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
            params.width = getResources().getDisplayMetrics().widthPixels -100;
            contentView.setLayoutParams(params);
            bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
            bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
            bottomDialog.setCanceledOnTouchOutside(true);
            bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            bottomDialog.show();

            TextView tv_cancle = bottomDialog.findViewById(R.id.tv_cancle);
            TextView tv_Shopowner = bottomDialog.findViewById(R.id.tv_Shopowner);
            TextView tv_staff = bottomDialog.findViewById(R.id.tv_staff);
            tv_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    bottomDialog.dismiss();
                }
            });

            tv_Shopowner.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    roleId=6;
                    tvAccountType.setText("店长账号");
                    bottomDialog.dismiss();
                }
            });

            tv_staff.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    roleId=7;
                    tvAccountType.setText("员工账号");
                    bottomDialog.dismiss();
                }
            });

        }

    private boolean isNull(){
        if (roleId==0){
            RxToast.error("请选择员工类型");
            return false;
        }
        if (TextUtils.isEmpty(etAccountName.getText().toString().trim())){
            RxToast.error("请填写员工姓名");
            return false;
        }
        if (TextUtils.isEmpty(etPhoneNum.getText().toString().trim())){
            RxToast.error("请填写手机号码");
            return false;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString().trim())){
            RxToast.error("请填写账户密码");
            return false;
        }
        return true;
    }

    //修改、添加员工接口
    private void initOkgo(){
        if (isNull()){
            OkGo.<String>post(Config.s + "llc/doupshopmanorEmp")
                    .tag(this)
                    .params("uid", id)
                    .params("username",etAccountName.getText().toString().trim())
                    .params("tel", etPhoneNum.getText().toString().trim())
                    .params("roleId", roleId)
                    .params("password", etPassword.getText().toString().trim())
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            BaseBean baseBean=gson.fromJson(response.body(),BaseBean.class);
                            if (baseBean.isIsSuccess()) {
                                RxToast.success(baseBean.getOutputMessage());
                                finish();
                            }else {
                                RxToast.error(baseBean.getOutputMessage());
                            }
                        }
                    });
        }
    }

     //获取要编辑的员工信息
        private void getStaffMessages() {
            OkGo.<String>post(Config.s + "llc/toupshopmanorEmp")
                    .tag(this)
                    .params("uid", id)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            StaffMsgBean bean=gson.fromJson(response.body(),StaffMsgBean.class);
                            if (bean.isIsSuccess()){
                                etAccountName.setText(bean.getData().getUsername());
                                etPhoneNum.setText(bean.getData().getTel());
                                etPassword.setText(bean.getData().getPassword());
                                roleId=bean.getData().getRoleId();
                                if (bean.getData().getRoleId()==6){
                                    tvAccountType.setText("店长账号");
                                }else {
                                    tvAccountType.setText("员工账号");
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }


    private void buyVipDialog() {
        final Dialog bottomDialog = new Dialog(this);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_buyvip, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.9);
        contentView.setLayoutParams(params);
        bottomDialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation2);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.show();

        TextView cancel = bottomDialog.findViewById(R.id.cancel);
        TextView submit = bottomDialog.findViewById(R.id.submit);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomDialog.dismiss();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                OkGo.<String>post(Config.s + "llc/deleteShopKeeperOrEmp")
                        .tag(this)
                        .params("uid", id)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                Gson gson = new Gson();
                                BaseBean baseBean=gson.fromJson(response.body(),BaseBean.class);
                                if (baseBean.isIsSuccess()) {
                                    RxToast.success(baseBean.getOutputMessage());
                                    bottomDialog.dismiss();
                                    finish();
                                }else {
                                    RxToast.error(baseBean.getOutputMessage());
                                }
                            }
                        });
            }
        });

    }

    //新增员工接口
    private void initOkgoAdd(){
        if (isNull()){
            OkGo.<String>post(Config.s + "llc/addShopKeeperOrEmp")
                    .tag(this)
                    .params("token", UserBean.getbToken(this))
                    .params("tel", etPhoneNum.getText().toString().trim())
                    .params("username", etAccountName.getText().toString().trim())
                    .params("password", etPassword.getText().toString().trim())
                    .params("roleId", roleId)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            BaseBean baseBean=gson.fromJson(response.body(),BaseBean.class);
                            if (baseBean.isIsSuccess()) {
                                RxToast.success(baseBean.getOutputMessage());
                                finish();
                            }else {
                                RxToast.error(baseBean.getOutputMessage());
                            }
                        }
                    });
        }
    }

}
